package com.gdssoft.pdaweb.jobs;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gdssoft.core.util.DateUtil;
import com.gdssoft.pdaweb.dao.CrmWorkOrderDao;
import com.gdssoft.pdaweb.model.CrmWorkOrder;
import com.gdssoft.pdaweb.service.CrmService;

public class CrmFeedbackJob {

	private transient final Log logger = LogFactory.getLog(getClass());
	
	private CrmService crmService;
	private CrmWorkOrderDao crmWorkOrderDao;
	
	public CrmFeedbackJob(CrmService crmService, CrmWorkOrderDao crmWorkOrderDao) {
		this.crmService = crmService;
		this.crmWorkOrderDao = crmWorkOrderDao;
	}
	
	public void doJob() {
		logger.info("CrmFeedbackJob execute start");
		
		List<CrmWorkOrder> list = crmWorkOrderDao.listForFeedback();
		for (CrmWorkOrder crmWorkOrder : list) {
			boolean bu = crmService.syncWordOrderStatus(crmWorkOrder);
			
			if (bu) {
				crmWorkOrder.setFeedbackDt(DateUtil.dateFormat(new Date()));
				crmWorkOrder.setUpdatePerson("system");
				crmWorkOrder.setUpdateDt(new Date());
				crmWorkOrderDao.saveOrUpdate(crmWorkOrder);
			}
		}
		
		logger.info("CrmFeedbackJob execute end");
	}
}
